package com.atguigu.tingshu.live.api;

import com.atguigu.tingshu.common.login.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.live.service.LiveRoomService;
import com.atguigu.tingshu.model.live.LiveRoom;
import com.atguigu.tingshu.vo.live.LiveRoomVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/live/liveRoom")
@SuppressWarnings({"unchecked", "rawtypes"})
public class LiveRoomApiController {
	@Autowired
	private LiveRoomService liveRoomService;

	//api/live/liveRoom/getCurrentLive

	@Login
	@GetMapping("getCurrentLive")
	public Result<LiveRoom> getCurrentLive(){
		LiveRoom liveRoom=this.liveRoomService.getOne(new LambdaQueryWrapper<LiveRoom>()
				.eq(LiveRoom::getUserId, AuthContextHolder.getUserId()).gt(LiveRoom::getExpireTime,new Date()));
		return Result.ok(liveRoom);
	}
	// liveRoom/saveLiveRoom
	@PostMapping("saveLiveRoom")
	@Login
	public Result<LiveRoom> saveLiveRoom(@RequestBody LiveRoomVo liveRoomVo){
		LiveRoom liveRoom= this.liveRoomService.saveLiveRoom(liveRoomVo);
		return Result.ok(liveRoom);
	}
	@GetMapping("getById/{liveRoomId}")
	public  Result<LiveRoom> getById(@PathVariable Long liveRoomId){
		LiveRoom liveRoom = this.liveRoomService.getById(liveRoomId);
		return Result.ok(liveRoom);
	}
	@GetMapping("findLiveList")
	public Result<List<LiveRoom>> findLiveList(){
		List<LiveRoom> liveRooms = this.liveRoomService.list(
//				new LambdaQueryWrapper<LiveRoom>().gt(LiveRoom::getExpireTime, new Date())
		);
		return Result.ok(liveRooms);
	}
 }

